﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Core.Domain;
using KL.Store.Web.Models;
using KL.Store.Data;
using KL.Store.Core;

namespace KL.Store.Web.Controllers
{
    [RequireHttps]
    [Authorize]
    public class AddressController : KL.Store.Core.Web.BaseController
    {
        #region Index

        public ActionResult Index()
        {
            
            return View();
        }

        #endregion

        #region Get

        public ActionResult Get()
        {
            AddressListModel addressModel = new AddressListModel();
            addressModel.AddressList = dbAddress.GetList(eStoreUser.AccountId, eStoreUser.AccountType);
            if (addressModel.AddressList == null)
            {
                addressModel.AddressList = new List<Address>();
            }
            return PartialView("Get", addressModel);
        }

        #endregion

        #region Delete

        public ActionResult Delete(short Id)
        {
            if (eStoreUser.AccountId > 0)
            {
                dbAddress.Delete(eStoreUser.AccountId, Id);
            }

            return Get();
        }

        #endregion

        #region Update

        [AllowAnonymous]
        public ActionResult Update(AddressModel aModel, short? Id2, short? isCheckout)
        {
            ViewBag.isCheckout = (isCheckout.HasValue && isCheckout == 1) ? 1 : 0;
            if (Id2.HasValue)
            {
                AddressModel addressModel = new AddressModel();


                if (Id2.Value > 0)
                {
                    Address address = dbAddress.Get(eStoreUser.AccountId, (short)Id2);
                    if (address != null)
                    {
                        Mapper.MapDomainToViewModel2(address, addressModel);
                    }
                }
                else
                {
                    addressModel.AddressName = "ADRESİM";
                    addressModel.Name = eStoreUser.FirstName + " " + eStoreUser.LastName;
                    addressModel.Phone = eStoreUser.Mobile;
                }

                addressModel.CityList = dbCity.GetList(1);

                return PartialView("Update", addressModel);
            }
            else
            {
                string Message = "";
                if (string.IsNullOrEmpty(aModel.AddressName))
                {
                    Message = Message + ", Kısa Ad";
                }
                if (string.IsNullOrEmpty(aModel.Phone))
                {
                    Message = Message + ", Telefon";
                }
                if (string.IsNullOrEmpty(aModel.Address1))
                {
                    Message = Message + ", Adres";
                }
                if (string.IsNullOrEmpty(aModel.County))
                {
                    Message = Message + ", İlçe";
                }
                if (aModel.City == 0)
                {
                    Message = Message + ", İl";
                }
                if (Message != "")
                {
                    Message = "Bilgileri kontrol ediniz" + Message;
                    aModel.Message = Message;

                    //aModel.CityList = dbCity.GetList(1);
                    return JavaScript("userAddressErrorMessage('" + Message + "');");
                }
                else
                {
                    if (aModel.AddressId == 0)
                    {
#warning Ülke seçimi eklenecek şu an default Turkiye
                        dbAddress.Insert(eStoreUser.AccountId, aModel.AddressName, aModel.Name, aModel.TaxOffice, aModel.TaxNo, aModel.Address1, aModel.Address2, aModel.County, aModel.PostalCode, 1 /*aModel.Country*/, aModel.City, eStoreUser.AccountId, aModel.Phone, eStoreUser.AccountType);
                        return JavaScript("$.fancybox.close();");
                    }
                    else
                    {
                        dbAddress.Update(eStoreUser.AccountId, aModel.AddressId, aModel.AddressName, aModel.Name, aModel.TaxOffice, aModel.TaxNo, aModel.Address1, aModel.Address2, aModel.County, aModel.PostalCode, 1 /*aModel.Country*/, aModel.City, eStoreUser.AccountId, aModel.Phone);
                        return JavaScript("$.fancybox.close();");
                    }
                }
            }
        }

        #endregion
    }
}
